import _ from 'lodash';
// 公用的
//#region 单位角色
export const companyRole_data = [{
        id: 'TRAVEL_AGENCY',
        val: null,
        text: '旅行社',
        other: 'c-red'
    },
    {
        id: 'CAR_TEAM',
        val: null,
        text: '车队',
        other: 'c-red'
    },
    {
        id: 'RENT_COMPANY',
        val: null,
        text: '租赁公司',
        other: 'c-red'
    },
    {
        id: 'CONFERENCE_COMPANY',
        val: null,
        text: '会务公司',
        other: 'c-green'
    }
]
//#endregion

//#region 图片地址  此处可以通过判断环境变量设置开发与生产的不同地址
export const ImageUrl = 'https://cba360.com'
//  `${import.meta.env.VITE_SERVE.replace('kcbServiceApi', 'kcbservice-admin')}`
//#endregion

//#region 上传图片的地址
// 测试
// export const ImageUrl_upload = '/api/back/file/myUploadFiles'
// 线上
export const ImageUrl_upload = '/kcbServiceApi/back/file/myUploadFiles'
//#endregion

//#region 文件类型
export const FileType = [{
        "id": "ROUTE_TITLE_IMG",
        "val": null,
        "text": "行程标题图片",
        "other": "c-gray"
    },
    {
        "id": "ROUTE_POSTER_IMG",
        "val": null,
        "text": "行程海报图片",
        "other": "c-green"
    },
    {
        "id": "ROUTE_EXPLAIN_IMG",
        "val": null,
        "text": "行程说明图片",
        "other": "c-green"
    },
    {
        "id": "ROUTE_DETAIL_CON_IMG",
        "val": null,
        "text": "行程详情内容图片",
        "other": "c-green"
    },
    {
        "id": "COMPANY_DETAIL_IMG",
        "val": null,
        "text": "单位详情图片",
        "other": "c-green"
    },
    {
        "id": "HEAD_IMG",
        "val": null,
        "text": "头像图片",
        "other": "c-green"
    },
    {
        "id": "IDCARD_FRONT_IMG",
        "val": null,
        "text": "身份证正面照",
        "other": "c-green"
    },
    {
        "id": "IDCARD_BACK_IMG",
        "val": null,
        "text": "身份证背面照",
        "other": "c-green"
    },
    {
        "id": "BUSINESS_IMG",
        "val": null,
        "text": "经营许可证",
        "other": "c-green"
    },
    {
        "id": "BUSLICENSE_IMG",
        "val": null,
        "text": "工商营业执照",
        "other": "c-green"
    },
    {
        "id": "BACK_SET_DATA_IMG",
        "val": null,
        "text": "其他",
        "other": "c-blue"
    },
    {
        "id": "HEAD_BANNER_IMG",
        "val": null,
        "text": "首页轮播图",
        "other": "c-blue"
    },
    {
        "id": "INVITATION_IMG",
        "val": null,
        "text": "邀请码图片",
        "other": "c-blue"
    },
    {
        "id": "NOTICE_DETAIL_IMG",
        "val": null,
        "text": "公告详情图片",
        "other": "c-blue"
    },
    {
        "id": "GROUP_QRCODE_IMG",
        "val": null,
        "text": "群二维码图片",
        "other": "c-blue"
    },
    {
        "id": "PERSONAL_QRCODE_IMG",
        "val": null,
        "text": "个人微信二维码图片",
        "other": "c-blue"
    }
]

//#endregion

//#region 高德地图
export const securityJsCode = 'aac7d76ba0a5dbde956c5694d87ee792' // 安全秘钥

export const key = 'bb28dd7fa15afc231362b033739d9420' // key
//#endregion

//#region 获取设置类型
export const DataSetType = [{
        "id": "HOTEL",
        "val": null,
        "text": "酒店",
        "other": "c-gray"
    },
    {
        "id": "TRAIN",
        "val": null,
        "text": "火车",
        "other": "c-green"
    },
    {
        "id": "AIRPORT",
        "val": null,
        "text": "机场",
        "other": "c-green"
    },
    {
        "id": "BUS",
        "val": null,
        "text": "汽车",
        "other": "c-green"
    },
    {
        "id": "DIY",
        "val": null,
        "text": "自定义",
        "other": "c-green"
    },
    {
        "id": "OTHER",
        "val": null,
        "text": "其他",
        "other": "c-green"
    }
]


//#endregion


//#region 导入文件地址
export const importCusCheckInData = '/api/company/cusCheckInfo/importCusCheckInData'
//#endregion
//#region 导入银行帐文件地址
export const importfeeBtl = '/kcbServiceApi/company/financeCenter/importfeeBtl'
//#endregion
//#region 线型数据转树形数据
/**
 * 线型数据转树形数据
 * @param list 欲处理的数据列表
 * @param pname 父级标志名称 如：pid
 */
export const line2treeData = (list, pname) => {

    if (!list || list.length == 0) return list;

    let map = {};
    // 遍历第一遍，以id为键，保存起来，方便获取
    list.forEach(it => {
        map[it.id] = it;
    });
    // console.log('1.处理后的数据', map);

    // 遍历第二遍，为父级添加子集   
    for (let it in map) {
        let c = map[it],
            p = map[c[pname]];

        if (p) {
            if (p.children) {
                p.children.push(c);
            } else {
                p.children = [c];
            }
        } else { // 不存在，则保存为顶级（去掉这里，不然elementui的cascader组件取不了没有子集的选项值）
            // c.children = [];
        }
    }

    // console.log('2.处理后的数据', map);

    // 第三遍遍历，排除不是父级的数据
    let reslist = [];
    for (let it in map) {
        let c = map[it];
        // 父级不存在的对象保存
        if (!map[c[pname]]) reslist.push(map[it]);
    }

    // console.log('3.处理后的数据', reslist);
    return reslist;
}

//#endregion

/**
 * 
 * @param {*} idName    查找的标志属性名称（标志的值才能作为查找依据）
 * @param {*} pidName   查找的标志父级属性名称（一般是pid）
 * @param {*} sid       标志属性开始值
 * @param {*} eid       标志属性结束值（默认0或者空，因为结束即找到了顶级）
 */
export const findP2CArr = (list, idName, pidName, sid, eid) => {
    let res = []; // 结果数组
    console.log(list)
    if (!list || list.length == 0) return res;

    let map = {};
    // 遍历第一遍，以id为键，保存起来，方便获取
    list.forEach(it => {
        map[it[idName]] = it;
    });
    console.log(map, 'map')
    // 获取-开始id的对象
    let sobj = map[sid];

    if (!sobj) {
        return res; // 没有开始id对象，也返回
    } else {
        res.push(sobj); /// 存储开始对象
    }

    // 获取开始对象的pid
    let pid = sobj[pidName];
    // 获取父级对象
    let pobj = map[pid];
    if (!pid || pid == 0 || !pobj) return res;

    // 满足条件
    while (pid && pid != 0 && pobj) {
        // 父级id不为空或者0且父级得存在于map中（因为为空或者0则是顶级）
        res.unshift(pobj); // 插入数组第一位

        if (eid && eid == pid) {
            pid = 0; // 终止循环
        } else {
            pid = pobj[pidName];
            pobj = map[pid];
        }
    }
    console.log('查询层级关联数据对象数组', res);
    return res;
}


/**
    * 给表单参数对象设置值，数组参数
    * @param {*} ps 设置的参数对象
    * @param {*} val 设置的值

    */
export const setFmArr = (ps = [], val) => {
    if (val.length == 0) {
        ps.length = 0;
    } else {
        ps.length = 0; // 清空一下
        val.forEach(it => {
            ps.push(it);
        });
    }
    return ps;
}


/**
 * 提取身份证信息
 * @param {String} idCard 身份证号码
 * @param {*} separator 分隔字符 eg：‘-’，‘/’（默认）
 * @returns {age=年龄, birthday=生日年月日, gender=性别}
 * if (!/(^\d{8}(0\d|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/.test(idCard)) {
 */
export const getIdCardInfo = (idCard, separator = '/') => {
    if (!/(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/.test(idCard)) {
        this.qinfo(`${idCard}不是一个身份证号码`);
        return {
            age: '',
            birthday: '',
            gender: ''
        };
    }

    // 提取 idCard 中的字符
    const idSubstr = (s, e) => idCard.substr(s, e);

    // 拼接日期
    const splice = d => d.join(separator);

    // 获取出生年月日 性别（0 女 1 男）
    let birthday, gender;
    if (idCard.length === 18) {
        birthday = splice([idSubstr(6, 4), idSubstr(10, 2), idSubstr(12, 2)]);
        gender = idSubstr(-2, 1) & 1;
    } else {
        birthday = splice(idSubstr(6, 2), idSubstr(8, 2), idSubstr(10, 2));
        gender = idSubstr(-1, 1) & 1;
    }

    // 获取年龄（实岁）
    const birthDate = new Date(birthday);
    const newDate = new Date();
    const year = newDate.getFullYear();
    let age = year - birthDate.getFullYear();
    if (newDate < new Date(splice([year, birthday.substring(5)]))) {
        age--;
    };

    return {
        age,
        birthday,
        gender
    };
}


// 导出excel
export const downFiles = (blob, fileName) => {
    const url = window.URL.createObjectURL(new Blob([blob]))
    console.log(url)
    const link = document.createElement('a')
    link.href = url
    link.setAttribute('download', `${fileName}.xlsx`)
    document.body.appendChild(link)
    link.click()
    link.remove()
    window.URL.revokeObjectURL(link.href)


}

// 深度拷贝对象

export const scopy = (obj) => {
    return _.cloneDeep(obj);
}


/**
 * 获取-浏览器可视高度
 * @param h 减去的高度
 */
export const fullHeightAPI = (h = 0) => {
    return document.documentElement.clientHeight - h;
}
